
/*##############################################################################
 Thomas M. Flaherty
 Email: t1flaher@ucsd.edu
 Date Created: 09/29/2019
 Comment(s):
		(i) 
 Input File(s): 
		(1) CS_replicationdata.dta (from Colantone and Stanig 2018)
		(2) WID_posttax_p99p100.csv
		(3) WID_posttax_p90p100.csv
 Output File(s): 
		(1) NA
##############################################################################*/


clear    
clear matrix
set matsize 10000
set more off
capture log close 
set scrollbufsize 300000 // (set scrollbufsize will take effect the next time you launch Stata)

*###############################################################################
* change directory 
*###############################################################################

cd "~/Dropbox/replication_files/

*###############################################################################
* import and merge raw inequality data from WID (downloaded Sept 29th, 2019)
*###############################################################################


import delimited "data/input/world_inequality_database/WID_posttax_top1.csv", clear
rename v1 country
rename v2 year
rename v3 posttax_top1share
label var posttax_top1share "Top 1% share of post-tax national income , adults, equal split" 
save "data/output/WID_posttax_top1.dta", replace

import delimited "data/input/world_inequality_database/WID_posttax_top10.csv", clear
rename v1 country
rename v2 year
rename v3 posttax_top10share
label var posttax_top10share "Top 10% share of post-tax national income , adults, equal split" 
save "data/output/WID_posttax_top10.dta", replace

merge 1:1 country year using "data/output/WID_posttax_top1.dta"
drop _merge

egen country_id = group(country)
tsset country_id year
gen posttax_top1share_lag1 = L.posttax_top1share
gen posttax_top10share_lag1 = L.posttax_top10share

save "data/output/WID_posttax_inequality.dta", replace 

use "data/input/CS_replicationdata.dta", clear

merge m:1 country year using "data/output/WID_posttax_inequality.dta"
drop _merge

*###############################################################################
* Var transformations
*###############################################################################

* transformations (not done in Colantone and Stanig)
gen radical_right_ln = ln(radical_right)
gen import_shock_ln = ln(import_shock+.18) // see https://amstat.tandfonline.com/doi/full/10.1080/19466315.2017.1369900#.XuZ6gC2z3mE 
gen instrument_for_shock_ln = ln(instrument_for_shock)
gen posttax_top10share_lag1_ln = ln(posttax_top10share_lag1) 
gen posttax_top1share_lag1_ln = ln(posttax_top1share_lag1)

* standardize the IVs that do NOT have intuitive units (i.e. do not standardize dummies)
egen zimport_shock_ln = std(import_shock_ln)
egen zinstrument_for_shock_ln = std(instrument_for_shock_ln)
egen zposttax_top10share_lag1_ln = std(posttax_top10share_lag1_ln)
egen zposttax_top1share_lag1_ln = std(posttax_top1share_lag1_ln)

* List of DVs:
* cog_nationalism median_nat_autarchy cog_nat_autarchy radical_right

*###############################################################################
* Table
*###############################################################################

		***** Replication regressions: From Table 1, specifications (9) and (10) 
* country-year FE
areg radical_right import_shock  ,  vce(cluster nuts2_year) absorb(country_year)
estimates store m1, title("(1)")
* 2SLS:
ivreg2 radical_right (import_shock=instrument_for_shock)   i.country_year, cluster(nuts2_year) first
estimates store m2, title("(2)")

		***** Add log transformations to control skew + 2wayFE to allow for country-year variables
* 2-way FE
areg radical_right_ln import_shock_ln  i.year,  vce(cluster nuts2_year) absorb(country)
estimates store m3, title("(3)")
* 2SLSs
ivreg2 radical_right_ln (import_shock_ln=instrument_for_shock_ln)  i.year i.country_id, cluster(nuts2_year) first
estimates store m4, title("(4)")

		***** add inequality
* 2-way FE
areg radical_right_ln import_shock_ln  posttax_top1share_lag1_ln i.year,  vce(cluster nuts2_year) absorb(country) // this and below uses country fe to identify inequality
estimates store m5, title("(5)")
* 2SLS 
ivreg2 radical_right_ln (import_shock_ln=instrument_for_shock_ln)  posttax_top1share_lag1_ln i.year i.country_id, cluster(nuts2_year) first
estimates store m6, title("(6)")

		***** multilevel models
* no interaction 
mixed radical_right_ln c.import_shock_ln posttax_top1share_lag1_ln   i.year || country: posttax_top1share_lag1_ln || nuts2: import_shock_ln ,  vce(cluster country)
estimates store m7, title("(7)")
* interaction
mixed radical_right_ln c.import_shock_ln##c.posttax_top1share_lag1_ln   i.year || country: posttax_top1share_lag1_ln || nuts2: import_shock_ln ,  vce(cluster country)
estimates store m8, title("(8)")

estout m1 m2 m3 m4 m5 m6 m7 m8, starlevels( * 0.10 ** 0.05 *** 0.010) cells(b(star fmt(3)) se(par fmt(2)))legend label varlabels(_cons Constant) ///
   stats(N r2_a df_r, fmt(0 3 0) label(N Adj.R-sqr df.resid)) title("") wrap dropped  ///   style(tex) 
   order(import_shock  posttax_top1share_lag1 import_shock_ln  posttax_top1share_lag1_ln ) noomitted nobaselevels  

   
*###############################################################################
* Coeff plot of model (3) + (8) 
*###############################################################################

gen interaction = zimport_shock_ln*zposttax_top1share_lag1_ln // needed for coefplot
label var interaction "top 1% share x log(China shock)"
label var zimport_shock_ln "log(China shock)"
label var zposttax_top1share_lag1_ln "top 1% share"

* NOTE: continuous variable in standardized units for interpretability and comparability. 

areg radical_right_ln zimport_shock_ln,  vce(cluster nuts2_year) absorb(country_year)
estimates store CS2018
mixed radical_right_ln zimport_shock_ln zposttax_top1share_lag1_ln interaction   i.year || country: zposttax_top1share_lag1_ln || nuts2: zimport_shock_ln ,  vce(cluster country)
estimates store IO2020

coefplot CS2018 IO2020, drop(_cons) xline(0) 	///
	graphregion(color(white)) ///
	scheme(s2mono) ///
	keep(zimport_shock_ln zposttax_top1share_lag1_ln  interaction ) 
graph export "figures/CS_coefplot.png", replace



*###############################################################################
* Marginal effects plot; (preferred) multilevel models
*###############################################################################

* top 1 percent; posttax LINEAR
mixed radical_right_ln c.zimport_shock_ln##c.posttax_top1share_lag1   i.year || country: posttax_top1share_lag1 || nuts2: zimport_shock_ln ,  vce(cluster country)
margins, dydx(zimport_shock_ln) at(posttax_top1share_lag1 = ( 0.04(0.01)0.12)) // 
marginsplot, /// base command
	yline(0, lpattern(dash) lcolor(black))  ///
	legend(off) ///
	level(95) /// CI
	recastci(rspike) /// CI display options
	ci1opts(lcolor(black)) /// CI color
	plotregion(fcolor(white) lcolor(gs10) lwidth(med)) /// background color and border outline
	plot1opts(mcolor(white) mlcolor(black) lcolor(black)) ///
	xscale(range(.018 .15) ) /// 
	xlabel(.015(0.01).15, labcolor(gs4) tlcolor() tlwidth(thin) labsize(small) nogrid) /// x-axis labels coloration 
	ylabel(, labcolor(gs4) tlcolor() tlwidth(thin) labsize(small) nogrid angle(horizontal)) ///
	xtitle("Top 1% share", color(black)) /// 
	ytitle("Effect of log imports on log populist vote share", color(black)) ///
	title("") ///
		addplot(hist posttax_top1share_lag1, percent /// Use "discrete" if actual distribution is desired
			blcolor(black) fcolor(black) ///
			yaxis(2) /// creates a second y-axis
			yscale(alt lcolor() axis(2)) /// 
			ylabel(0 " " 20 " " 40 " " 60 " " 80 " " 100 " ", /// minimizing the height of histogram 
				labcolor() axis(2) tlcolor(black) tlwidth(thin) labsize(small) tl(0)) /// label options for 2nd axis and removes ticks 
			ytitle("", axis(2)) /// no title on 2nd axis 
			legend(off) ///
		)
graph export "figures/CS_marginsplot_linear_multilevel.png", replace		

* top 1 percent; posttax NON-LINEAR		
mixed radical_right_ln c.zimport_shock_ln##c.posttax_top1share_lag1##c.posttax_top1share_lag1##c.posttax_top1share_lag1   i.year || country: posttax_top1share_lag1 || nuts2: zimport_shock_ln ,  vce(cluster country)
margins, dydx(zimport_shock_ln) at(posttax_top1share_lag1 = ( 0.04(0.01)0.12)) //  
marginsplot, /// base command
	yline(0, lpattern(dash) lcolor(black))  ///
	legend(off) ///
	level(95) /// CI
	recastci(rspike) /// CI display options
	ci1opts(lcolor(black)) /// CI color
	plotregion(fcolor(white) lcolor(gs10) lwidth(med)) /// background color and border outline
	plot1opts(mcolor(white) mlcolor(black) lcolor(black)) ///
	xscale(range(.02 .14) ) /// 
	xlabel(.02(0.01).14, labcolor(gs4) tlcolor() tlwidth(thin) labsize(small) nogrid) /// x-axis labels coloration 
	ylabel(, labcolor(gs4) tlcolor() tlwidth(thin) labsize(small) nogrid angle(horizontal)) ///
	xtitle("Top 1% share", color(black)) /// 
	ytitle("Effect of log imports on log populist vote share", color(black)) ///
	title("") ///
		addplot(hist posttax_top1share_lag1, percent /// Use "discrete" if actual distribution is desired
			blcolor(black) fcolor(black) ///
			yaxis(2) /// creates a second y-axis
			yscale(alt lcolor() axis(2)) /// 
			ylabel(0 " " 20 " " 40 " " 60 " " 80 " " 100 " ", /// minimizing the height of histogram 
				labcolor() axis(2) tlcolor(black) tlwidth(thin) labsize(small) tl(0)) /// label options for 2nd axis and removes ticks 
			ytitle("", axis(2)) /// no title on 2nd axis 
			legend(off) ///
		)		
graph export "figures/CS_marginsplot_nonlinear_multilevel.png", replace		
		

*###############################################################################
* Marginal effects plot; (alternative) country and year FE models
*###############################################################################

* top 1 percent; posttax LINEAR
areg radical_right_ln c.zimport_shock_ln##c.posttax_top1share_lag1   i.year,  vce(cluster nuts2_year) absorb(country)
margins, dydx(zimport_shock_ln) at(posttax_top1share_lag1 = ( 0.03(0.01)0.12)) // (-0.4(0.4)0.4))   
marginsplot, /// base command
	yline(0, lpattern(dash) lcolor(black))  ///
	legend(off) ///
	level(95) /// CI
	recastci(rspike) /// CI display options
	ci1opts(lcolor(black)) /// CI color
	plotregion(fcolor(white) lcolor(gs10) lwidth(med)) /// background color and border outline
	plot1opts(mcolor(white) mlcolor(black) lcolor(black)) ///
	xscale(range(.02 .14) ) /// 
	xlabel(.02(0.01).14, labcolor(gs4) tlcolor() tlwidth(thin) labsize(small) nogrid) /// x-axis labels coloration 
	ylabel(, labcolor(gs4) tlcolor() tlwidth(thin) labsize(small) nogrid angle(horizontal)) ///
	xtitle("Top 1% share", color(black)) /// 
	ytitle("Effect of log imports on log populist vote share", color(black)) ///
	title("") ///
		addplot(hist posttax_top1share_lag1, percent /// Use "discrete" if actual distribution is desired
			blcolor(black) fcolor(black) ///
			yaxis(2) /// creates a second y-axis
			yscale(alt lcolor() axis(2)) /// 
			ylabel(0 " " 20 " " 40 " " 60 " " 80 " " 100 " ", /// minimizing the height of histogram 
				labcolor() axis(2) tlcolor(black) tlwidth(thin) labsize(small) tl(0)) /// label options for 2nd axis and removes ticks 
			ytitle("", axis(2)) /// no title on 2nd axis 
			legend(off) ///
		)		
graph export "figures/CS_marginsplot_linear_FE.png", replace		

* top 1 percent; posttax NON-LINEAR
areg radical_right_ln c.zimport_shock_ln##c.posttax_top1share_lag1##c.posttax_top1share_lag1    i.year,  vce(cluster nuts2_year) absorb(country)
margins, dydx(zimport_shock_ln) at(posttax_top1share_lag1 = ( 0.03(0.01)0.12)) // (-0.4(0.4)0.4))   
marginsplot, /// base command
	yline(0, lpattern(dash) lcolor(black))  ///
	legend(off) ///
	level(95) /// CI
	recastci(rspike) /// CI display options
	ci1opts(lcolor(black)) /// CI color
	plotregion(fcolor(white) lcolor(gs10) lwidth(med)) /// background color and border outline
	plot1opts(mcolor(white) mlcolor(black) lcolor(black)) ///
	xscale(range(.02 .14) ) /// 
	xlabel(.02(0.01).14, labcolor(gs4) tlcolor() tlwidth(thin) labsize(small) nogrid) /// x-axis labels coloration 
	ylabel(, labcolor(gs4) tlcolor() tlwidth(thin) labsize(small) nogrid angle(horizontal)) ///
	xtitle("Top 1% share", color(black)) /// 
	ytitle("Effect of log imports on log populist vote share", color(black)) ///
	title("") ///
		addplot(hist posttax_top1share_lag1, percent /// Use "discrete" if actual distribution is desired
			blcolor(black) fcolor(black) ///
			yaxis(2) /// creates a second y-axis
			yscale(alt lcolor() axis(2)) /// 
			ylabel(0 " " 20 " " 40 " " 60 " " 80 " " 100 " ", /// minimizing the height of histogram 
				labcolor() axis(2) tlcolor(black) tlwidth(thin) labsize(small) tl(0)) /// label options for 2nd axis and removes ticks 
			ytitle("", axis(2)) /// no title on 2nd axis 
			legend(off) ///
		)	
graph export "figures/CS_marginsplot_nonlinear_FE.png", replace		


*###############################################################################
* End
*###############################################################################











